package ua.yandex.lockfree;

import java.math.BigInteger;
import java.util.concurrent.atomic.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SequenceGenerator {

    private static final BigInteger MULTIPLIER = BigInteger.valueOf(2);
    private final AtomicReference<BigInteger> element
            = new AtomicReference<BigInteger>(BigInteger.ONE);

    public BigInteger next() {
        BigInteger value;
        BigInteger next;
        do {
            value = element.get();
            next = value.multiply(MULTIPLIER);
        } while (!element.compareAndSet(value, next));
        return value;
    }
}
